<template>
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle"
               @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams2"
      isSelectColumn
      datakey="records"
      :onRowSelectChange="selectChange"
      :onColumnsChange="columns => this.columns = columns"
      :isExportExcel2="true"
      :exportFileName="'电池回收网点.xlsx'"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="addHandle">新增</el-button>
        <el-button size="small" :disabled="!rowSelectData.length" type="primary" icon="el-icon-tickets"
                   @click="updateStatusHandle(true)">启用
        </el-button>
        <el-button size="small" :disabled="!rowSelectData.length" type="primary" icon="el-icon-document-remove"
                   @click="updateStatusHandle(false)">不启用
        </el-button>
        <el-button size="small" type="primary" icon="el-icon-tickets"
                   @click="visibleImport = true">导入
        </el-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visibleForm" :title="dialogTitle" destroyOnClose width="800px"
                :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <branchEditDetail :handleType="handleType" :value="formData" @close="closeHandler"/>
    </BaseDialog>
    <BaseDialog :visible.sync="visibleImport" width="80vw" title="电池回收网点批量导入"
                destroyOnClose
                :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '50px' }">
      <ExcelImport
        ref="import"
        template-file-name="电池回收网点导入模板.xlsx"
        :importRules="importRules"
        :onComplete="importComplete"
        @close="visibleImport = false"
      />
    </BaseDialog>
  </div>
</template>

<script>
import {getList,postUpdateStatusByIds,putImportConfirm} from '@/api/partsmanage/batteryTrace/branchEdit';
import {sfqyArr} from './constant';
import branchEditDetail from './branchEditDetail';
import {dictionary} from '@/utils/dictMixin';
import ExcelImport from '@/components/ExcelImport';

export default {
  name: "batteryPlantIndex",
  components: {
    branchEditDetail,
    ExcelImport
  },
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getList,
      fetchParams: {},
      visibleForm: false,
      handleType: '',
      formData: {},
      dialogTitle: '回收网点维护',
      rowSelectData: [],
      visibleImport:false,
      importRules: this.initImportRuleFun()
    };
  },
  computed: {
    fetchParams2() {
      return {
        ...this.fetchParams
      }
    }
  },
  methods: {
    createTopFilterList() {
      return [
        {type: 'INPUT', label: '回收网点代码', fieldName: 'code', placeholder: '请输入'},
        {type: 'INPUT', label: '回收网点名称', fieldName: 'name', placeholder: '请输入'},
        {type: 'SELECT', label: '是否启用', fieldName: 'xtqybz', itemList: sfqyArr, placeholder: '请选择'},
      ]
    },
    initImportRuleFun() {
      return [
        {
          title: '回收网点代码', prop: 'code',
          rule: {required: true, message: '回收网点代码不能为空'}
        },
        {title: '回收网点名称', prop: 'name',
          rule: {required: true, message: '回收网点名称不能为空'}
        },
        {
          title: '社会信用代码', prop: 'shxycode',
          rule: {required: true, message: '社会信用代码不能为空'},
        },
        {
          title: '是否启用', prop: 'xtqybz',
          rule: [
            {required: true, message: '是否启用不能为空'},
            {itemList: sfqyArr},
          ]
        },
      ]
    },
    /*导入*/
    importComplete(datas, callback) {
      putImportConfirm(datas).then(res => {
        if (res.resultCode == 200) {
          if (res.data.status) {
            this.visibleImport = false
            this.$notify.success({title: '提示信息', message: '批量导入成功'});
            callback()
            this.closeHandler('refresh')
          } else {
            callback(res.data.errorData)
          }
        } else {
          callback([{errormsg: '服务器运行异常'}])
        }
      })
    },
    createTableColumns() {
      return [
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 130,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row, 'edit')}>
                  编辑
                </el-button>
                <el-divider direction="vertical"/>
                <el-button size="mini" type="text" onClick={() => this.detailHandle(props.row, 'show')}>
                  明细
                </el-button>
              </div>
            );
          }
        },
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '回收网点代码',
          dataIndex: 'code',
          sorter: true,
        },
        {
          title: '回收网点名称',
          dataIndex: 'name',
          sorter: true,
        },
        {
          title: '社会信用代码',
          dataIndex: 'shxycode',
          sorter: true,
        },
        {
          title: '是否启用',
          dataIndex: 'xtqybz',
          width: 150,
          sorter: true,
          dictItems: sfqyArr,
        }
      ]
    },
    addHandle() {
      this.handleType = 'add';
      this.visibleForm = true
    },
    detailHandle(row, handleType) {
      this.handleType = handleType;
      this.formData = {
        id: row.id,
        data: {
          ...row
        }
      };
      this.visibleForm = true
    },
    async updateStatusHandle(available) {
      const res = await postUpdateStatusByIds({
        available,
        ids: this.rowSelectData.map(i => i.id)
      })
      if (res.resultCode == 200) {
        this.$notify.success({title: '提示信息', message: '操作成功'});
        // 重新加载
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    closeHandler(type) {
      this.visibleForm = false
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
    },
    selectChange(val) {
      this.rowSelectData = val;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, this.fetchParams, val);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
  }
}
</script>

<style scoped>

</style>
